package com.zmn.plat.admin.controller.channel.sp;


import com.alibaba.fastjson.JSON;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.KvDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.dto2.data.LayuiTableDO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.admin.controller.channel.ChannelBaseController;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.channel.Channel;
import com.zmn.plat.model.entity.channel.sp.ChannelSp;
import com.zmn.plat.model.entity.channel.sp.ChannelSpQuery;
import com.zmn.plat.services.interfaces.channel.sp.ChannelSpService;
import com.zmn.sp.common.model.manage.SpServProviderDRO;
import com.zmn.sp.common.model.manage.SpServProviderQuery;
import com.zmn.sp.dubbo.interfaces.manage.SpManageListRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述: 渠道管理新增服务商配置
 *
 * @author jiafei
 * @version 1.0
 * @since 2021/6/7 17:57
 */
@Slf4j
@RestController
@RequestMapping("/channel/sp/")
public class ChannelSpListController extends ChannelBaseController {

    private static final String TAG = "渠道服务商基础服务接口";

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private SpManageListRemoteService spManageListRemoteService;

    @Resource
    private ChannelSpService channelSpService;

    /**
     * 描述: 查询所有的服务商(黑白名单中)
     *
     * @return org.springframework.web.servlet.ModelAndView
     * @author jiafei
     * @since 2021/6/7 18:02
     */
    @RequestMapping(value = "list")
    @RequiresPermissions("/channel/sp/list.action")
    public ModelAndView list(@ModelAttribute ChannelSpQuery query) {

        Channel obj = channelService.findByKey(query.getChannelId());
        // 渠道旋转的服务商
        List<ChannelSp> items = channelSpService.listPageByQuery(query);

        ModelAndView mav = new ModelAndView("channel/sp/list");
        mav.addObject("flag", 16);
        mav.addObject("addFlag", query.getAddFlag());
        mav.addObject("query", query);
        mav.addObject("obj", obj);
        // 所属平台
        mav.addObject("plats", GlobalConsts.getPlatList());
        // 渠道配置的服务商(黑白名单)
        mav.addObject("items", items);

        return mav;
    }

    /**
     * 描述: 跳转名单页面
     *
     * @param query   入参
     * @return org.springframework.web.servlet.ModelAndView
     * @author jiafei
     * @since 2021/6/8 19:22
     */
    @RequestMapping(value = "select")
    public ModelAndView selectServiceProvider(@ModelAttribute ChannelSpQuery query) {

        Channel obj = channelService.findByKey(query.getChannelId());

        ModelAndView mav = new ModelAndView("channel/sp/select/list");
        mav.addObject("addFlag", query.getAddFlag());
        mav.addObject("query", query);
        mav.addObject("obj", obj);

        return mav;
    }

    /**
     * 描述: 远程查询服务商
     *
     * @param query query
     * @return com.zmn.common.dto2.ResponseDTO<com.zmn.common.dto2.data.LayuiTableDO<com.zmn.sp.common.model.manage.SpServProviderDRO>>
     * @author jiafei
     * @since 2021/6/9 10:57
     */
    @ResponseBody
    @RequestMapping(value = "listPageByQuery")
    public ResponseDTO<LayuiTableDO<SpServProviderDRO>> listPageByQuery(@ModelAttribute SpServProviderQuery query) {

        log.info("[{}],远程:查询服务商，入参 query:{}", TAG, JSON.toJSONString(query));
        return spManageListRemoteService.listPageByQuery(query);
    }

    /**
     * 描述: 初始化实时搜索下拉框
     *
     * @param searchType searchType
     * @param key        key
     * @return com.zmn.common.dto2.ResponseDTO<java.util.List<com.zmn.common.dto.KvDTO<java.lang.Integer, java.lang.String>>>
     * @author jiafei
     * @since 2021/6/9 11:01
     */
    @ResponseBody
    @RequestMapping(value = "findSpProviderByKeyword")
    public ResponseDTO<List<KvDTO<Integer, String>>> findSpProviderByKeyword(Integer searchType, String key) {

        log.info("[{}],远程:初始化实时搜索下拉框，入参 searchType:{},key;{}", TAG, searchType, key);
        return spManageListRemoteService.findSpProviderByKeyword(searchType, key);
    }
}
